/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elaprendiz.ventanas.buscar;

import elaprendiz.controllers.CProducto;
import elaprendiz.gui.label.LabelRect;
import elaprendiz.modelbd.Producto;
import elaprendiz.modelgui.ModeloTablaProducto;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JOptionPane;

/**
 *
 * @author elaprendiz <http://www.elaprendiz.net63.net>
 */
public class BuscarProducto extends javax.swing.JPanel {

    /**
     * Creates new form BuscarProducto
     */
    private CProducto controllerProducto;
    private Producto producto;
    private ModeloTablaProducto mtp;
    private JOptionPane op;
    public BuscarProducto() {
        initComponents();
        mtp = new ModeloTablaProducto();
        this.tblProductos.setModel(mtp);
        this.lbAviso.setVisible(false);       
    }
    
     public BuscarProducto(int compra) {
        initComponents();
        CProducto productos = new CProducto();        
        mtp = new ModeloTablaProducto(productos.getRegistros(new Object[]{1}),compra);
        this.tblProductos.setModel(mtp);
        this.lbAviso.setVisible(false);       
    }

    public LabelRect getLbAviso() {
        return lbAviso;
    }

    private void getOptionPane()
    {
        if(op != null)
        {
            return;
        }
        Component pdr =this.getParent(); 
        while(pdr.getParent() != null)
        {
            if(pdr instanceof JOptionPane)
            {
                op = (JOptionPane)pdr;
                break;
            }            
            pdr = pdr.getParent();
        }
    }

    public Producto getProducto() {
        return producto;
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        buttonGroup1 = new javax.swing.ButtonGroup();
        panelImage1 = new elaprendiz.gui.panel.PanelImage();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        rbCodigoBarras = new javax.swing.JRadioButton();
        rbNombre = new javax.swing.JRadioButton();
        txtDato = new elaprendiz.gui.textField.TextField(new Dimension(400,50));
        bntBuscar = new elaprendiz.gui.button.ButtonRect();
        lbAviso = new elaprendiz.gui.label.LabelRect();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblProductos = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        btnAceptar = new elaprendiz.gui.button.ButtonRect();
        btnCancelar = new elaprendiz.gui.button.ButtonRect();

        setPreferredSize(new java.awt.Dimension(679, 339));
        setLayout(new java.awt.BorderLayout());

        panelImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/elaprendiz/resources/fondoazulceleste.jpg"))); // NOI18N
        panelImage1.setLayout(new java.awt.BorderLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setOpaque(false);
        jPanel1.setLayout(new java.awt.GridBagLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Buscar Por:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(8, 0, 0, 10);
        jPanel1.add(jLabel1, gridBagConstraints);

        rbCodigoBarras.setBackground(new java.awt.Color(51, 153, 255));
        buttonGroup1.add(rbCodigoBarras);
        rbCodigoBarras.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        rbCodigoBarras.setSelected(true);
        rbCodigoBarras.setText("Codigo de Barras");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 0, 5, 0);
        jPanel1.add(rbCodigoBarras, gridBagConstraints);

        rbNombre.setBackground(new java.awt.Color(51, 153, 255));
        buttonGroup1.add(rbNombre);
        rbNombre.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        rbNombre.setText("Nombre");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 9, 5, 0);
        jPanel1.add(rbNombre, gridBagConstraints);

        txtDato.setToolTipText("Dejar vacio y hacer Click \nen Buscar para mostrar\n todos los productos\n");
        txtDato.setPreferredSize(new java.awt.Dimension(180, 27));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 8;
        gridBagConstraints.ipadx = 55;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(8, 0, 0, 1);
        jPanel1.add(txtDato, gridBagConstraints);

        bntBuscar.setBackground(new java.awt.Color(51, 153, 255));
        bntBuscar.setText("Buscar");
        bntBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntBuscarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(8, 19, 0, 0);
        jPanel1.add(bntBuscar, gridBagConstraints);

        lbAviso.setBackground(new java.awt.Color(255, 0, 0));
        lbAviso.setForeground(new java.awt.Color(255, 255, 0));
        lbAviso.setText("Error: Seleccione un Producto");
        lbAviso.setPreferredSize(new java.awt.Dimension(235, 17));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 0);
        jPanel1.add(lbAviso, gridBagConstraints);

        panelImage1.add(jPanel1, java.awt.BorderLayout.PAGE_START);

        jPanel2.setOpaque(false);
        jPanel2.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setPreferredSize(new java.awt.Dimension(452, 250));

        tblProductos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblProductos);

        jPanel2.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel3.setOpaque(false);

        btnAceptar.setBackground(new java.awt.Color(51, 153, 255));
        btnAceptar.setText("Seleccionar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });
        jPanel3.add(btnAceptar);

        btnCancelar.setBackground(new java.awt.Color(51, 153, 255));
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        jPanel3.add(btnCancelar);

        jPanel2.add(jPanel3, java.awt.BorderLayout.PAGE_END);

        panelImage1.add(jPanel2, java.awt.BorderLayout.CENTER);

        add(panelImage1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void bntBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntBuscarActionPerformed
        if(!this.txtDato.getText().isEmpty())
        {
            controllerProducto = new CProducto();
            String cnom = null;
            if(this.rbCodigoBarras.isSelected())
            {
                cnom = "cod_barras";               
            }else if(this.rbNombre.isSelected())
            {
                cnom = "nombre";
            }
            mtp = new ModeloTablaProducto(controllerProducto.getRegistros(new String[]{"activo",cnom},new Object[]{1,this.txtDato.getText()}));
            this.tblProductos.setModel(mtp);
        }else{
            mtp = new ModeloTablaProducto();
            this.tblProductos.setModel(mtp);
        }

    }//GEN-LAST:event_bntBuscarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        getOptionPane();
        op.setValue(1);
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        if(tblProductos.getSelectedRow() != -1)
        {
            producto =(Producto)mtp.getFila(tblProductos.getSelectedRow());
            getOptionPane();
            op.setValue(1);
        }else{
           lbAviso.setVisible(true);
        }
    }//GEN-LAST:event_btnAceptarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private elaprendiz.gui.button.ButtonRect bntBuscar;
    private elaprendiz.gui.button.ButtonRect btnAceptar;
    private elaprendiz.gui.button.ButtonRect btnCancelar;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private elaprendiz.gui.label.LabelRect lbAviso;
    private elaprendiz.gui.panel.PanelImage panelImage1;
    private javax.swing.JRadioButton rbCodigoBarras;
    private javax.swing.JRadioButton rbNombre;
    private javax.swing.JTable tblProductos;
    private elaprendiz.gui.textField.TextField txtDato;
    // End of variables declaration//GEN-END:variables
}
